﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using CarolLib.Control;
using DispatchAssist.Service;
using System.Web.Services.Protocols;

namespace DispatchAssist.AppHost
{
    //[ModuleContract("c5aa03aa-2f4e-4052-903c-7cfc6d1b5e35", "系统管理", "联系人机构管理")]
    [Obsolete]
    public partial class ContactGroupManage : MdiUserControl
    {
        AuthService authService = new AuthService();
        int rowID = -1;
        public ContactGroupManage()
            : base()
        {
            InitializeComponent();

        }
        public ContactGroupManage(XmlNode node)
            : this()
        {
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgrdContactGroup.SelectedRows == null || dgrdContactGroup.SelectedRows.Count <= 0 || dgrdContactGroup.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择要删除的记录行！");
                return;
            }
            if (MessageBox.Show("确认删除此公司？", "删除确认", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                return;
            }
            rowID = this.dgrdContactGroup.CurrentRow.Index;
            Guid groupID = new Guid(System.Convert.ToString(dgrdContactGroup.CurrentRow.Cells["SMSContactGroupID"].Value));
            if (authService.DeleteSMSContactGroup(groupID))
            {
                MessageBox.Show("删除成功！");
                BindGroupList();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string tag = System.Convert.ToString((sender as Button).Tag);
            if (string.IsNullOrEmpty(this.txtGroupName.Text))
            {
                MessageBox.Show("请输入公司名称！");
                return;
            }
            if (string.IsNullOrEmpty(this.txtGroupCode.Text))
            {
                MessageBox.Show("请输入公司代码！");
                return;
            }
            if (tag == "create")
            {
                SaveNewGroup();
            }
            else if (!string.IsNullOrEmpty(tag))
            {
                SaveEditGroup(tag);
            }

        }

        private void SaveEditGroup(string groupID)
        {
            SMSContactGroup group = new SMSContactGroup();
            //BusinessLogical.Model.SeatUser user = new BusinessLogical.Model.SeatUser();
            group.SMSContactGroupID = new Guid(groupID);
            group.GroupName = this.txtGroupName.Text;
            group.GroupCode = this.txtGroupCode.Text;
            DataAccessReturnValue result = authService.UpdateSMSContactGroup(group);
            if (result == DataAccessReturnValue.无法更新)
            {
                MessageBox.Show("该公司代码已存在！");
            }
            else if (result == DataAccessReturnValue.更新成功)
            {
                MessageBox.Show("保存成功！");
                BindGroupList();
                this.grpEditGroup.Visible = false;
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
        }

        private void SaveNewGroup()
        {
            SMSContactGroup group = new SMSContactGroup();
            //BusinessLogical.Model.SeatUser user = new BusinessLogical.Model.SeatUser();
            group.GroupName = this.txtGroupName.Text;
            group.GroupCode = this.txtGroupCode.Text;
            DataAccessReturnValue result = authService.CreateSMSContactGroup(group);
            if (result == DataAccessReturnValue.无法更新)
            {
                MessageBox.Show("该公司代码已存在！");
            }
            else if (result == DataAccessReturnValue.更新成功)
            {
                MessageBox.Show("添加成功！");
                BindGroupList();
                this.grpEditGroup.Visible = false;
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.grpEditGroup.Visible = false;
        }
        private void btnCreateEdit_Click(object sender, EventArgs e)
        {
            if ((sender as Button).Tag.ToString() == "create")
            {
                CreateGroup();
            }
            else if ((sender as Button).Tag.ToString() == "edit")
            {
                EditGroup();
            }
        }

        private void EditGroup()
        {
            if (this.dgrdContactGroup.SelectedRows == null || dgrdContactGroup.SelectedRows.Count <= 0 || dgrdContactGroup.SelectedRows.Count > 1)
            {
                MessageBox.Show("请选择要编辑的记录行！");
                return;
            }
            rowID = this.dgrdContactGroup.CurrentRow.Index;
            Guid groupID = new Guid(System.Convert.ToString(dgrdContactGroup.CurrentRow.Cells["SMSContactGroupID"].Value));
            this.txtGroupName.Text = System.Convert.ToString(dgrdContactGroup.CurrentRow.Cells["GroupName"].Value);
            this.txtGroupCode.Text = System.Convert.ToString(dgrdContactGroup.CurrentRow.Cells["GroupCode"].Value);
            this.btnSave.Tag = System.Convert.ToString(groupID);
            this.grpEditGroup.Visible = true;
        }

        private void CreateGroup()
        {
            btnSave.Tag = "create";
            this.txtGroupCode.Text = string.Empty;
            this.txtGroupName.Text = string.Empty;
            this.grpEditGroup.Visible = true;
        }

        private void ContactGroupManage_Load(object sender, EventArgs e)
        {

        }

        public override void Onload()
        {
            authService.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();

            BindGroupList();
            base.Onload();
        }

        private void BindGroupList()
        {
            try
            {
                List<SMSContactGroup> group = authService.GetContactGroup().ToList<SMSContactGroup>();
                this.dgrdContactGroup.AutoGenerateColumns = false;
                dgrdContactGroup.DataSource = group;
                this.grpEditGroup.Visible = false;

            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

    }
}
